#include <assert.h>
#include "math.h"
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
/* widgets:
Layer_MainMenu2
Background_Mainmenu
CoverFlow_Mainmenu
Container_Config
Text_MM_Setting
PopupButton_Config
Container_MainMenu29
Text_MM_Page29
Text_MM_State29
Text_MM_Name2_29
Text_MM_Name1_29
PopupButton_MainMenu29
Container_MainMenu28
Text_MM_Page28
Text_MM_State28
Text_MM_Name2_28
Text_MM_Name1_28
PopupButton_MainMenu28
Container_MainMenu27
Text_MM_Page27
Text_MM_State27
Text_MM_Name2_27
Text_MM_Name1_27
PopupButton_MainMenu27
Container_MainMenu26
Text_MM_Page26
Text_MM_State26
Text_MM_Name2_26
Text_MM_Name1_26
PopupButton_MainMenu26
Container_MainMenu25
Text_MM_Page25
Text_MM_State25
Text_MM_Name2_25
Text_MM_Name1_25
PopupButton_MainMenu25
Container_MainMenu24
Text_MM_Page24
Text_MM_State24
Text_MM_Name2_24
Text_MM_Name1_24
PopupButton_MainMenu24
Container_MainMenu23
Text_MM_Page23
Text_MM_State23
Text_MM_Name2_23
Text_MM_Name1_23
PopupButton_MainMenu23
Container_MainMenu22
Text_MM_Page22
Text_MM_State22
Text_MM_Name2_22
Text_MM_Name1_22
PopupButton_MainMenu22
Container_MainMenu21
Text_MM_Page21
Text_MM_State21
Text_MM_Name2_21
Text_MM_Name1_21
PopupButton_MainMenu21
Container_MainMenu20
Text_MM_Page20
Text_MM_State20
Text_MM_Name2_20
Text_MM_Name1_20
PopupButton_MainMenu20
Container_MainMenu19
Text_MM_Page19
Text_MM_State19
Text_MM_Name2_19
Text_MM_Name1_19
PopupButton_MainMenu19
Container_MainMenu18
Text_MM_Page18
Text_MM_State18
Text_MM_Name2_18
Text_MM_Name1_18
PopupButton_MainMenu18
Container_MainMenu17
Text_MM_Page17
Text_MM_State17
Text_MM_Name2_17
Text_MM_Name1_17
PopupButton_MainMenu17
Container_MainMenu16
Text_MM_Page16
Text_MM_State16
Text_MM_Name2_16
Text_MM_Name1_16
PopupButton_MainMenu16
Container_MainMenu15
Text_MM_Page15
Text_MM_State15
Text_MM_Name2_15
Text_MM_Name1_15
PopupButton_MainMenu15
Container_MainMenu14
Text_MM_Page14
Text_MM_State14
Text_MM_Name2_14
Text_MM_Name1_14
PopupButton_MainMenu14
Container_MainMenu13
Text_MM_Page13
Text_MM_State13
Text_MM_Name2_13
Text_MM_Name1_13
PopupButton_MainMenu13
Container_MainMenu12
Text_MM_Page12
Text_MM_State12
Text_MM_Name2_12
Text_MM_Name1_12
PopupButton_MainMenu12
Container_MainMenu11
Text_MM_Page11
Text_MM_State11
Text_MM_Name2_11
Text_MM_Name1_11
PopupButton_MainMenu11
Container_MainMenu10
Text_MM_Page10
Text_MM_State10
Text_MM_Name2_10
Text_MM_Name1_10
PopupButton_MainMenu10
Container_MainMenu9
Text_MM_Page9
Text_MM_State9
Text_MM_Name2_9
Text_MM_Name1_9
PopupButton_MainMenu9
Container_MainMenu8
Text_MM_Page8
Text_MM_State8
Text_MM_Name2_8
Text_MM_Name1_8
PopupButton_MainMenu8
Container_MainMenu7
Text_MM_Page7
Text_MM_State7
Text_MM_Name2_7
Text_MM_Name1_7
PopupButton_MainMenu7
Container_MainMenu6
Text_MM_Page6
Text_MM_State6
Text_MM_Name2_6
Text_MM_Name1_6
PopupButton_MainMenu6
Container_MainMenu5
Text_MM_Page5
Text_MM_State5
Text_MM_Name2_5
Text_MM_Name1_5
PopupButton_MainMenu5
Container_MainMenu4
Text_MM_Page4
Text_MM_State4
Text_MM_Name2_4
Text_MM_Name1_4
PopupButton_MainMenu4
Container_MainMenu3
Text_MM_Page3
Text_MM_State3
Text_MM_Name2_3
Text_MM_Name1_3
PopupButton_MainMenu3
Container_MainMenu2
Text_MM_Page2
Text_MM_State2
Text_MM_Name2_2
Text_MM_Name1_2
PopupButton_MainMenu2
Container_MainMenu1
Text_MM_Page1
Text_MM_State1
Text_MM_Name2_1
Text_MM_Name1_1
PopupButton_MainMenu1
Container_MainMenu0
Text_MM_Page0
Text_MM_State0
Text_MM_Name2_0
Text_MM_Name1_0
PopupButton_MainMenu0
*/

//根据序号跳转界面
void GotoLayerByKetVol(uint8_t DeviceKeyVol);
void SetMenuAngle(int Angle, uint8_t Page);
void GoToPage(uint8_t PageNum);
static ITUBackground* Background_Mainmenu2;
static ITUWheelBackground* WheelBackground_MM2[6];
static ITUContainer* Container_MM2_Button[DeviceNumMax];
static ITUPopupButton* PopupButton_MM2[DeviceNumMax];
static ITUText* Text_MM2_UP[DeviceNumMax];
static ITUText* Text_MM2_DOWN[DeviceNumMax];
static ITUText* Text_MM2_Page;
static bool Is_HaveDevice = false;
static uint8_t PageNow = 1,PageMax = 1;
bool MainMenu2OnEnter(ITUWidget* widget, char* param)
{
	uint8_t i, PageNum_D = 0;
	char PageNum[3], Page[10];
	char TempName[64], TempNum[64];
	bool PageVisible[6] = { false };
	UTF8_TWO_LINE DeviceTempName;
	if (!Background_Mainmenu2)
	{
		//加载小部件
		Background_Mainmenu2 = ituSceneFindWidget(&theScene, "Background_Mainmenu2");
		assert(Background_Mainmenu2);
		Text_MM2_Page = ituSceneFindWidget(&theScene, "Text_MM2_Page");
		assert(Text_MM2_Page);
		//Container_MM2_Page
		for (i = 1; i <= 6; i++)
		{
			strcpy(TempName, "WheelBackground_MM2_");
			strcat(TempName, itoa(i, TempNum, 10));
#ifdef WIN32
			printf("Load MainMenu2:%s\n", TempName);
#endif
			WheelBackground_MM2[i-1] = ituSceneFindWidget(&theScene, TempName);
			assert(WheelBackground_MM2[i-1]);
		}
		//Container_MM2_Button
		for (i = 0; i < DeviceNumMax; i++)
		{
			strcpy(TempName, "Container_MM2_Button");
			strcat(TempName, itoa(i, TempNum, 10));
#ifdef WIN32
			printf("Load MainMenu2:%s\n", TempName);
#endif
			Container_MM2_Button[i] = ituSceneFindWidget(&theScene, TempName);
			assert(Container_MM2_Button[i]);
		}
		//Button
		for (i = 0; i < DeviceNumMax; i++)
		{
			strcpy(TempName, "PopupButton_MM2_");
			strcat(TempName, itoa(i, TempNum, 10));
#ifdef WIN32
			printf("Load MainMenu2:%s\n", TempName);
#endif
			PopupButton_MM2[i] = ituSceneFindWidget(&theScene, TempName);
			assert(PopupButton_MM2[i]);
		}
		//Text_MM2_UP
		for (i = 0; i < DeviceNumMax; i++)
		{
			strcpy(TempName, "Text_MM2_UP_");
			strcat(TempName, itoa(i, TempNum, 10));
#ifdef WIN32
			printf("Load MainMenu2:%s\n", TempName);
#endif
			Text_MM2_UP[i] = ituSceneFindWidget(&theScene, TempName);
			assert(Text_MM2_UP[i]);
		}
		//Text_MM2_DOWN
		for (i = 0; i < DeviceNumMax; i++)
		{
			strcpy(TempName, "Text_MM2_DOWN_");
			strcat(TempName, itoa(i, TempNum, 10));
#ifdef WIN32
			printf("Load MainMenu2:%s\n", TempName);
#endif
			Text_MM2_DOWN[i] = ituSceneFindWidget(&theScene, TempName);
			assert(Text_MM2_DOWN[i]);
		}
	}
	if (Sys.Language == MG_LANGUAGE_CHS)
	{
		for (i = 0; i < DeviceNumMax; i++)
		{
			Text_MM2_UP[i]->fontIndex = 5;
			Text_MM2_UP[i]->boldSize = 1;
			Text_MM2_DOWN[i]->fontIndex = 5;
			Text_MM2_DOWN[i]->boldSize = 1;
		}
		Text_MM2_Page->fontIndex = 2;
		Text_MM2_Page->boldSize = 1;
		ituTextSetFontSize(Text_MM2_Page, 20);
	}
	else if (Sys.Language == MG_LANGUAGE_ENG)
	{
		for (i = 0; i < DeviceNumMax; i++)
		{
			Text_MM2_UP[i]->fontIndex = 4;
			Text_MM2_UP[i]->boldSize = 0;
			Text_MM2_DOWN[i]->fontIndex = 4;
			Text_MM2_DOWN[i]->boldSize = 0;
		}
		Text_MM2_Page->fontIndex = 7;
		Text_MM2_Page->boldSize = 0;
		ituTextSetFontSize(Text_MM2_Page, 18);
	}
	else if (Sys.Language == MG_LANGUAGE_ZHTW)
	{
		for (i = 0; i < DeviceNumMax; i++)
		{
			Text_MM2_UP[i]->fontIndex = 8;
			Text_MM2_UP[i]->boldSize = 0;
			Text_MM2_DOWN[i]->fontIndex = 8;
			Text_MM2_DOWN[i]->boldSize = 0;
		}
		Text_MM2_Page->fontIndex = 8;
		Text_MM2_Page->boldSize = 0;
		ituTextSetFontSize(Text_MM2_Page, 14);
	}
	ituWidgetSetVisible(Background_Mainmenu2, true);//设置为显示

	for (i = 1; i <= DeviceNumMax; i++)//获取总页数
	{
		if (TFT_Device[i].DeviceIsAllocation == true)
			PageNum_D = i;
	}
	(PageNum_D % 5 == 0) ? (PageNum_D = PageNum_D / 5) : (PageNum_D=PageNum_D / 5 + 1);
	PageMax = PageNum_D;
	itoa(PageMax, PageNum, 10);//将总页数转成字符串
	//页码显示
	itoa(PageNow, Page, 10);
	strcat(Page, "/");
	strcat(Page, PageNum);
	ituTextSetString(Text_MM2_Page, Page);

	//对每个按键进行编辑
	Is_HaveDevice = false;
	for (i = 1; i <= DeviceNumMax; i++)
	{
		if (TFT_Device[i].DeviceIsAllocation == true)
		{
			Is_HaveDevice = true;
			if (TFT_Device[i].DeviceEventType == 1)//设备名称拆解
				DeviceTempName = Get_UTF8_Two_Line(TFT_Device[i].TFTSCEQ.TFT_Device.DeviceName, TFT_Device[i].TFTSCEQ.TFT_Device.DeviceNameLen);//获取合理的换行字符串
			else//场景名称拆解
				DeviceTempName = Get_UTF8_Two_Line(TFT_Device[i].TFTSCEQ.TFT_Scene.DeviceName, TFT_Device[i].TFTSCEQ.TFT_Scene.DeviceNameLen);//获取合理的换行字符串
			ituTextSetString(Text_MM2_UP[i - 1], DeviceTempName.DeviceName1);
			ituTextSetString(Text_MM2_DOWN[i - 1], DeviceTempName.DeviceName2);
		}
		else
		{
			if (Sys.Language == MG_LANGUAGE_CHS)
			{
				ituTextSetString(Text_MM2_UP[i - 1], "暂未");
				ituTextSetString(Text_MM2_DOWN[i - 1], "配置");
			}
			else if (Sys.Language == MG_LANGUAGE_ENG)
			{
				ituTextSetString(Text_MM2_UP[i - 1], "Not");
				ituTextSetString(Text_MM2_DOWN[i - 1], "configured");
			}
			else if (Sys.Language == MG_LANGUAGE_ZHTW)
			{
				ituTextSetString(Text_MM2_UP[i - 1], "暫未");
				ituTextSetString(Text_MM2_DOWN[i - 1], "配置");
			}
		}
	}
	if (Is_HaveDevice == false)//没有设备
	{
		PageMax = 1;
		PageNow = 1;
		GoToPage(PageNow);
	}

	Set_Rotate_Block(120);//设置间隔刻度
    return true;
}

bool MainMenu2OnLeave(ITUWidget* widget, char* param)
{
	ituWidgetSetVisible(Background_Mainmenu2, false);//设置为隐藏
    return true;
}

bool MainMenu2OnTimer(ITUWidget* widget, char* param)
{
	static int WheelAngle = 0, WheelAngle_L = 0;
	char PageNum[3], Page[10];
	static uint8_t WheelRatate = 2;
	uint8_t i, PageNum_D = 0;
	int temp, Grade;
	UTF8_TWO_LINE DeviceTempName;

	//用户操作
	if (Action_Cmd.User_Action == Anticlockwise_Rotation)
	{
		if (WheelBackground_MM2[PageNow - 1]->value % 72 == 0)//在刻度上
		{
			WheelAngle = WheelBackground_MM2[PageNow - 1]->value - 15;
			if (WheelAngle < 0)WheelAngle = 360 + WheelAngle;
			ituWheelBackgroundSetValue(WheelBackground_MM2[PageNow - 1], WheelAngle);
		}
		Clear_User_Action();
	}
	else if (Action_Cmd.User_Action == Clockwise_Rotation)
	{
		if (WheelBackground_MM2[PageNow - 1]->value % 72 == 0)//在刻度上
		{
			WheelAngle = WheelBackground_MM2[PageNow - 1]->value + 15;
			if (WheelAngle >360)WheelAngle = WheelAngle - 360 + 15;
			ituWheelBackgroundSetValue(WheelBackground_MM2[PageNow - 1], WheelAngle);
		}
		Clear_User_Action();
	}
	else if (Action_Cmd.User_Action == Left_Slide)
	{
		PageNow <= 1 ? PageNow = 1 : PageNow--;//切换页面
		GoToPage(PageNow);
		Clear_User_Action();
	}
	else if (Action_Cmd.User_Action == Right_Slide)
	{
		PageNow >= PageMax ? PageNow = PageMax : PageNow++;//切换页面
		GoToPage(PageNow);
		Clear_User_Action();
	}

	//左转右转检测
	WheelAngle = WheelBackground_MM2[PageNow-1]->value;
	if ((WheelAngle - WheelAngle_L) >= 300 || WheelAngle < WheelAngle_L)//逆时针
		WheelRatate = 0;
	else if (WheelAngle > WheelAngle_L)//顺时针
		WheelRatate = 1;
	WheelAngle_L = WheelAngle;
	//UI动作计算
	if (MessageFlag[MSG_TOUCH_DOWN] == OFF)
	{
		temp = WheelAngle / 72;
		Grade = WheelAngle - temp * 72;//获得刻度余角度
		if (WheelRatate == 0)//逆时针
		{
			if (Grade != 0)//非刚好在刻度上
			{
				Grade = (0.2*Grade) + 1;//获得加速度
				WheelAngle = WheelAngle - Grade;//获得将要转达的角度
				if (WheelAngle < 0)WheelAngle = 360 + WheelAngle;
			}
		}
		else if (WheelRatate == 1)//顺时针
		{
			if (Grade != 0)//非刚好在刻度上
			{
				Grade = (-0.2*Grade) + 15.4;//获得加速度
				WheelAngle = WheelAngle + Grade;//获得将要转达的角度
				if (WheelAngle >= 360)WheelAngle = WheelAngle-360;
			}
		}
		SetMenuAngle(WheelAngle, PageNow);
	}
	//页码显示
	itoa(PageMax, PageNum, 10);//将总页数转成字符串
	itoa(PageNow, Page, 10);
	strcat(Page, "/");
	strcat(Page, PageNum);
	ituTextSetString(Text_MM2_Page, Page);
	//设备具体状态显示
	if (MessageFlag[MSG_GUI_CONFIG_UPDATE] == ON)
	{
		for (i = 1; i <= DeviceNumMax; i++)//获取总页数
		{
			if (TFT_Device[i].DeviceIsAllocation == true)
				PageNum_D = i;
		}
		(PageNum_D % 5 == 0) ? (PageNum_D = PageNum_D / 5) : (PageNum_D = PageNum_D / 5 + 1);
		PageMax = PageNum_D;
		itoa(PageMax, PageNum, 10);//将总页数转成字符串
		//页码显示
		itoa(PageNow, Page, 10);
		strcat(Page, "/");
		strcat(Page, PageNum);
		ituTextSetString(Text_MM2_Page, Page);
		//对每个按键进行编辑
		Is_HaveDevice = false;
		for (i = 1; i <= DeviceNumMax; i++)
		{
			if (TFT_Device[i].DeviceIsAllocation == true)
			{
				Is_HaveDevice = true;
				if (TFT_Device[i].DeviceEventType == 1)//设备名称拆解
					DeviceTempName = Get_UTF8_Two_Line(TFT_Device[i].TFTSCEQ.TFT_Device.DeviceName, TFT_Device[i].TFTSCEQ.TFT_Device.DeviceNameLen);//获取合理的换行字符串
				else//场景名称拆解
					DeviceTempName = Get_UTF8_Two_Line(TFT_Device[i].TFTSCEQ.TFT_Scene.DeviceName, TFT_Device[i].TFTSCEQ.TFT_Scene.DeviceNameLen);//获取合理的换行字符串
				ituTextSetString(Text_MM2_UP[i - 1], DeviceTempName.DeviceName1);
				ituTextSetString(Text_MM2_DOWN[i - 1], DeviceTempName.DeviceName2);
			}
			else
			{
				printf("Device No.%d is not exist! \n", i);
				if (Sys.Language == MG_LANGUAGE_CHS)
				{
					ituTextSetString(Text_MM2_UP[i - 1], "暂未");
					ituTextSetString(Text_MM2_DOWN[i - 1], "配置");
				}
				else if (Sys.Language == MG_LANGUAGE_ENG)
				{
					ituTextSetString(Text_MM2_UP[i - 1], "Not");
					ituTextSetString(Text_MM2_DOWN[i - 1], "configured");
				}
				else if (Sys.Language == MG_LANGUAGE_ZHTW)
				{
					ituTextSetString(Text_MM2_UP[i - 1], "暫未");
					ituTextSetString(Text_MM2_DOWN[i - 1], "配置");
				}
			}
		}
		if (Is_HaveDevice == false)//没有设备
		{
			PageMax = 1;
			PageNow = 1;
			GoToPage(PageNow);
		}
		MessageFlag[MSG_GUI_CONFIG_UPDATE] = OFF;
	}
	//进入测试模式
	if (MessageFlag[MSG_HARDWARE_TEST] == ON)
	{
		MessageFlag[MSG_HARDWARE_TEST] = OFF;
		ITULayer* Layer_TouchTest = ituSceneFindWidget(&theScene, "Layer_TouchTest");
		assert(Layer_TouchTest);
		ituLayerGoto(Layer_TouchTest);
	}
	//切换彩单类型
	if (MessageFlag[MSG_GUI_SWITCH_MAINMENU] == ON)
	{
		MessageFlag[MSG_GUI_SWITCH_MAINMENU] = OFF;
		GotoMainMenu();
	}
    return true;
}

bool PopupButton_MainMenu2_On_Press(ITUWidget* widget, char* param)
{
	uint8_t PressKeyNum;
	PressKeyNum = atoi(param);
	DisplayValue.TFT_Display_Device = PressKeyNum;
	MessageFlag[MSG_GET_DEVICE_STATE] = ON;
	MessageFlag[MSG_GET_DEVICE_CONFIG] = ON;
	GotoLayerByKetVol(DisplayValue.TFT_Display_Device);
    return true;
}
//跳转到指定页
void GoToPage(uint8_t PageNum)
{
	uint8_t i;
	for (i = 0; i < 6; i++)
	{
		if (i == PageNum - 1)
		{
			ituWidgetSetVisible(WheelBackground_MM2[i], true);//设置为显示
		}
		else
		{
			ituWidgetSetVisible(WheelBackground_MM2[i], false);//设置为显示
		}
	}
}
//设置旋转角度
void SetMenuAngle(int Angle,uint8_t Page)
{
	uint8_t CalculateX, CalculateY,ButtonAngle0;
	uint8_t i,j;

	//赋值角度
	ituWheelBackgroundSetValue(WheelBackground_MM2[PageNow - 1], Angle);

	//计算每个按钮坐标
	j = 0;
	for (i = (5 * (Page - 1)); i < (5 * (Page - 1) + 5); i++)
	{
		//计算坐标  圆心坐标+圆周半径*cos(角度+对应图像偏移角度+整体偏移角度)-控件宽度高度/2；
		CalculateX = 120 + 75 * cos((Angle + j * 72 + 270)*3.14 / 180) - 45;
		CalculateY = 120 + 75 * sin((Angle + j * 72 + 270)*3.14 / 180) - 30;
		ituWidgetSetX(Container_MM2_Button[i], CalculateX);
		ituWidgetSetY(Container_MM2_Button[i], CalculateY);
		ituWidgetSetColor(Text_MM2_UP[i], 255, 255, 255, 255);
		ituWidgetSetColor(Text_MM2_DOWN[i], 255, 255, 255, 255);
		if (Sys.Language == MG_LANGUAGE_CHS)
		{
			ituTextSetFontSize(Text_MM2_UP[i], 24);
			ituTextSetFontSize(Text_MM2_DOWN[i], 24);
		}
		else if (Sys.Language == MG_LANGUAGE_ENG)
		{
			ituTextSetFontSize(Text_MM2_UP[i], 18);
			ituTextSetFontSize(Text_MM2_DOWN[i], 18);
		}
		else if (Sys.Language == MG_LANGUAGE_ZHTW)
		{
			ituTextSetFontSize(Text_MM2_UP[i], 20);
			ituTextSetFontSize(Text_MM2_DOWN[i], 20);
		}
		j++;
	}

	//获取在顶部按键的偏移序号
	if (Angle % 72 == 0)
	{
		ButtonAngle0 = 5-(Angle / 72);
		if (ButtonAngle0 == 5)ButtonAngle0 = 0;
		ituWidgetSetColor(Text_MM2_UP[5 * (Page - 1) + ButtonAngle0], 255, 12, 108, 255);
		ituWidgetSetColor(Text_MM2_DOWN[5 * (Page - 1) + ButtonAngle0], 255, 12, 108, 255);
		if (Sys.Language == MG_LANGUAGE_CHS)
		{
			ituTextSetFontSize(Text_MM2_UP[5 * (Page - 1) + ButtonAngle0], 30);
			ituTextSetFontSize(Text_MM2_DOWN[5 * (Page - 1) + ButtonAngle0], 30);
		}
		else if (Sys.Language == MG_LANGUAGE_ENG)
		{
			ituTextSetFontSize(Text_MM2_UP[5 * (Page - 1) + ButtonAngle0], 18);
			ituTextSetFontSize(Text_MM2_DOWN[5 * (Page - 1) + ButtonAngle0], 18);
		}
		else if (Sys.Language == MG_LANGUAGE_ZHTW)
		{
			ituTextSetFontSize(Text_MM2_UP[5 * (Page - 1) + ButtonAngle0], 20);
			ituTextSetFontSize(Text_MM2_DOWN[5 * (Page - 1) + ButtonAngle0], 20);
		}
	}
}